﻿#include "mp4_pkt.h"


extern "C" const struct mov_buffer_t* mov_file_buffer(void);

bool init_av_track(struct mov_h264_test_t& ctx, int codec_type, unsigned char* s_extra_data)
{
	int extra_data_size = 0;
	if (ctx.track < 0)
	{
		if (codec_type == 0)
		{//H264 �ȴ� SPS��PPS �ķ��� 
			if (ctx.avc.nb_sps < 1 || ctx.avc.nb_pps < 1)
			{
				//ctx->ptr = end;
				return false; // waiting for sps/pps
			}
		}
		else if (codec_type == 1)
		{//H265 �ȴ�SPS��PPS�ķ��� 
			if (ctx.hevc.numOfArrays < 1)
			{
				//ctx->ptr = end;
				return false; // waiting for vps/sps/pps
			}
		}

		if (codec_type == 0)
			extra_data_size = mpeg4_avc_decoder_configuration_record_save(&ctx.avc, s_extra_data, 64 << 10);
		else if (codec_type == 1)
			extra_data_size = mpeg4_hevc_decoder_configuration_record_save(&ctx.hevc, s_extra_data, 64 << 10);

		if (extra_data_size <= 0)
		{
			// invalid AVCC
			return false;
		}

		// TODO: waiting for key frame ???
		if (codec_type == 0)
			ctx.track = mov_writer_add_video(ctx.mov, MOV_OBJECT_H264, ctx.width, ctx.height, s_extra_data, extra_data_size);
		else if (codec_type == 1)
			ctx.track = mov_writer_add_video(ctx.mov, MOV_OBJECT_HEVC, ctx.width, ctx.height, s_extra_data, extra_data_size);

		if (ctx.track < 0)
			return false;
	}
	return true;
}
